package com.wx.cloud.word.controller;

import com.wx.cloud.word.common.CommonResult;
import com.wx.cloud.word.model.Collect;
import com.wx.cloud.word.model.Like;
import com.wx.cloud.word.model.Page;
import com.wx.cloud.word.model.Word;
import com.wx.cloud.word.service.WordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/word")
public class WordController {

    @Autowired
    private WordService wordService;

    @GetMapping("/getSelectList")
    public CommonResult getSelectList(@RequestParam Map<String, Object> data){
        HashMap<String, Object> map = new HashMap<>();
        List<Word> words = wordService.getSelectList(data);
        int count = wordService.getSelectCount(data);
        Page page = new Page();
        page.setList(words);
        page.setTotal(count);
        map.put("list",words);
        map.put("count",count);
        return CommonResult.success(map);
    }

    @PostMapping("/addWord")
    public CommonResult addWord(@RequestBody Word word){
        HashMap<String, String> map = new HashMap<>();
        String result = wordService.addWord(word);
        map.put("result",result);
        return CommonResult.success(map);
    }

    @PostMapping("/delWord")
    public CommonResult delWord(@RequestBody Word word){
        HashMap<String, Integer> map = new HashMap<>();
        int result = wordService.delWord(word);
        map.put("result",result);
        return CommonResult.success(map);
    }

    @GetMapping("/getLikeByData")
    public CommonResult getLikeByData(@RequestParam Map<String, Object> data){
        HashMap<String, Object> map = new HashMap<>();
        Like like = wordService.getLikeByData(data);
        map.put("data",like);
        return CommonResult.success(map);
    }

    @PostMapping("/addLike")
    public CommonResult addLike(@RequestBody Like like){
        HashMap<String, String> map = new HashMap<>();
        String result = wordService.addLike(like);
        map.put("data",result);
        return CommonResult.success(map);
    }

    @PostMapping("/updateLike")
    public CommonResult updateLike(@RequestBody Like like){
        HashMap<String, Object> map = new HashMap<>();
        int result = wordService.updateLike(like);
        map.put("data",result);
        return CommonResult.success(map);
    }

    @GetMapping("/getCollectByData")
    public CommonResult getCollectByData(@RequestParam Map<String, Object> data){
        HashMap<String, Object> map = new HashMap<>();
        Collect collect = wordService.getCollectByData(data);
        map.put("data",collect);
        return CommonResult.success(map);
    }

    @PostMapping("/addCollect")
    public CommonResult addCollect(@RequestBody Collect collect){
        HashMap<String, String> map = new HashMap<>();
        String result = wordService.addCollect(collect);
        map.put("data",result);
        return CommonResult.success(map);
    }

    @PostMapping("/updateCollect")
    public CommonResult updateCollect(@RequestBody Collect collect){
        HashMap<String, Object> map = new HashMap<>();
        int result = wordService.updateCollect(collect);
        map.put("data",result);
        return CommonResult.success(map);
    }

    @PostMapping("/createDeletion")
    public CommonResult createDeletion(@RequestParam List<Integer> ids) throws Exception {
        return wordService.createDeletion(ids);
    }

    @GetMapping("/getMyWordList")
    public CommonResult getMyWordList(@RequestParam Map<String, Object> data){
        HashMap<String, Object> map = new HashMap<>();
        List<Word> words = wordService.getMyWordList(data);
        int count = wordService.getMyWordCount(data);
        Page page = new Page();
        page.setList(words);
        page.setTotal(count);
        map.put("list",words);
        map.put("count",count);
        return CommonResult.success(map);
    }

    @GetMapping("/getCollectList")
    public CommonResult getCollectList(@RequestParam Map<String, Object> data){
        HashMap<String, Object> map = new HashMap<>();
        List<Collect> collects = wordService.getCollectList(data);
        int count = wordService.getCollectCount(data);
        Page page = new Page();
        page.setList(collects);
        page.setTotal(count);
        map.put("list",collects);
        map.put("count",count);
        return CommonResult.success(map);
    }

}
